<?php

require 'facebook.php';
require 'config.inc.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $fbconfig['app_id'],
  'secret' => $fbconfig['secret_key'],
  'cookie' => true,
));

$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
  } catch (FacebookApiException $e) {
    echo "Somethings wrong";
  }
}

$uid = $_POST["uid"];
$access_token=$_POST["access_token"];
$offset=$_POST["offset"];
$limit=$_POST["limit"];


//$f = $facebook->api('/'.$uid.'/friends?access_token='.$access_token);
//$result = $facebook->api('https://api.facebook.com/method/fql.query?query=SELECT uid,name,pic_square FROM user WHERE uid in (select uid2 from friend where uid1='.$uid.') and is_app_user=\"true\"&access_token='.$access_token);

//https://api.facebook.com/method/fql.query?query=SELECT%20uid,name,pic_square%20FROM%20user%20WHERE%20uid%20in%20%28select%20uid2%20from%20friend%20where%20uid1=779340536%29%20and%20is_app_user=%22true%22&access_token=112984665429177|2d70706c48d18a9703ddad54-779340536|oKJ_I1YHhNkdt8HlgnAHyILaI48

try{
	$fql    =   'SELECT uid,name,pic_square FROM user WHERE is_app_user="true" and uid in (select uid2 from friend where uid1='.$uid.')';
	$param  =   array(
		'method'    => 'fql.query',
		'query'     => $fql,
		'callback'  => ''
	);
	$result   =   $facebook->api($param);
}catch(Exception $o){
	echo($o);
}

for($i=$offset,$txt="";$i<sizeof($result) && $i<$offset+$limit;$i++)
{
	$txt= $txt .'<div class="friend-block"> <img class="pic" src="'.$result[$i]["pic_square"].'" /> <a class="normal" onclick="friendProfile('.$result[$i]["uid"].')"> '.$result[$i]["name"].' </a></div>';
}

echo $txt;

//echo print_r($result);

?>

<div class="button">
<?php 
	$prev = $offset-$limit;
	$next = $offset+$limit;
	if($offset!=0)
	{
		echo "<a class='simple_button' onclick='javascript:friends($prev,$limit)'>Previous</a> ";
	}
	
	if($limit+$offset < sizeof($result)){
		echo "<a class='simple_button' onclick='javascript:friends($next,$limit)'>Next</a>";
	}
	
mysql_close($con);
?>
</div>
